<?php
namespace app\admin\controller;
use think\Request;

class Riddle extends Base
{
    
    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/riddle/login');
        }
        
        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }
            
            $result = db('customer')->where(['username' => $post['account']])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();

            // 查找不到
            if(! $result) {
                err('该账号不存在');
            }
            
            if($result['status'] != 1) {
                err('账号已禁用，请联系管理员');
            }
 
            // 密码不正确
            if(md5($post['pwd']) != $result['pwd']) {
                err('密码错误，请重新输入');
            }

            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);

            // 验证成功
            suc('验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/riddle/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }
   

    //配置管理页面
    public function configure()
    {
        $res = db('configure')->where(['admin_id'=>$this->accountId])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }

    //配置管理页面-提交
    public function configure_post()
    {
        $post = request()->only(['id','shre_number_title','pe_number_ttile','share_title','home_page','pe_number','share_number','bronze','silver','gold','platinum','diamonds','glory','king','checkpoint'],'post');
        
        //图片上传
        
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['share_img'] = $image;
        }
        
        $res = db('configure')->where(['id'=>$post['id'],'admin_id'=>$this->accountId])->update($post);
        
        if ($res) {
            $this->success('编辑成功','riddle/configure');
        }else{
            $this->error('编辑失败');
        }   
    }

    
    //分析记录列表
    public function user_list()
    {
        $post = request()->only(['title'],'post');
        
        if (!empty($post['title'])){
           
              $where['b.nickname'] = ['like','%'.$post['title'].'%'];

        }
        
        $where['a.admin_id'] = $this->accountId;
        $where['b.avatarurl'] = ['neq',""];
        $res = db('subject_statistics')->alias('a')->join('user b','a.user_id = b.id','left')
                ->where($where)
                ->field('a.user_id,b.avatarurl,b.nickname,a.fraction,a.share_number,a.game_number,a.create_time')
                ->order('a.create_time desc')
                ->paginate($this->page, false);
        $number = db('subject_statistics')->count('id');
                
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        $this->assign('number', $number);
        return $this->fetch();
    }


    //更多好玩列表
    public function extension_list()
    {
        $res = db('extension')->where(['admin_id'=>$this->accountId])->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }

    
    //更多好玩添加页面
    public function extension_add()
    {
        return $this->fetch();
    }

    //更多好玩添加-提交
    public function extension_add_post()
    {
        $post = request()->only(['type','title','content','url','sort_t','page_url'],'post');
        
        $post['create_time'] = date("Y-m-d");
        $post['admin_id'] = $this->accountId;
        //图片上传

        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['img'] = $image;
        }

        $res = db('extension')->insert($post);
        
        if ($res) {
            $this->success('添加成功','riddle/extension_list');
        }else{
            $this->error('添加失败');
        }
    }

    //更多好玩删除
    public function extension_del($id)
    {
        $res = db('extension')->delete($id);
        
        if ($res) {
            $this->success('删除成功','riddle/extension_list');
        }else{
            $this->error('删除失败');
        }
    }

    //更多好玩编辑页面
    public function extension_edit($id)
    {
        $res = db('extension')->where(['id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    //更多好玩编辑提交
    public function extension_edit_post()
    {
        $post = request()->only(['id','type','title','content','url','sort_t','page_url'],'post');
        
        $post['create_time'] = date("Y-m-d");
        //图片上传
        
        $file = new \app\admin\logic\File();
        $image = $file->uploadoss($this->accountId);
        
        if (!empty($image)){
            $post['img'] = $image;
        }
        $where['id'] = $post['id'];
        
        $res = db('extension')->where($where)->update($post);
        
        if ($res) {
            $this->success('编辑成功','riddle/extension_list');
        }else{
            $this->error('编辑失败');
        }
    }
    
    //上下架游戏
    public function extension_up($id,$type)
    {
        $res = db('extension')->where(['id'=>$id])->update(['sort'=>$type]);
        
        if ($res) {
            $this->success('操作成功','riddle/extension_list');
        }else{
            $this->error('操作失败');
        }
    }


    //推广游戏跳转次数统计
    public function statistics_list()
    {
        $post = request()->only(['title'],'post');
        if (!empty($post['title'])){
            $where['a.extension_id'] = $post['title'];
        }
        
        $where['a.admin_id'] = $this->accountId;
        $where['a.type'] = 2;

        $res = db('extension_statistics')
                ->alias('a')
                ->join('extension b','a.extension_id = b.id','left')
                ->where($where)
                ->field("count('a.id') as number,a.create_time,b.title,b.img")
                ->group('a.create_time,a.extension_id')
                ->paginate($this->page, false);
        
        $info = db('extension')->field('title,id')->where(['admin_id'=>$this->accountId])->select();        

        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        $this->assign('info',$info);
        return $this->fetch();
    }


    //单日统计
    public function day_list()
    {
        $res = db('all_statistics')->where(['admin_id'=>$this->accountId])
                ->group('day')
                ->field('day,sum(log_number) as log_number,sum(participate_number) as participate_number,sum(share_number) as share_number')
                ->select();
        
        $arr = db('user')->where(['admin_id'=>$this->accountId])->group('day')->field('day,count(id) as people_number')->select();
        
        foreach ($res as $k => $v){
            foreach ($arr as $key => $val){
                if ($v['day'] == $val['day']){
                    $res[$k]['people_number'] = $val['people_number'];
                }
            }
        }
        
        $this->assign('res',$res);
        return $this->fetch();
    }


    //题目管理
    public function subject()
    {
        $post = request()->only(['name'],'post');
        if (!empty($post['name'])){
            $where['question|answer'] = ['like','%'.$post['name'].'%'];
        }else{
            $where = [];
        }
        $res = db('riddle_title')->where($where)->field('id,prompt,question,numbers,answer,false_answer')->paginate($this->page, false);

        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res',$res);
        return $this->fetch();
    }

    //游戏题目编辑页面
    public function subject_edit($id)
    {
        $res = db('riddle_title')->where(['id'=>$id])->find();
        
        $this->assign('res',$res);
        return $this->fetch();
    }

    
    //游戏题目编辑提交
    public function subject_edit_post($id)
    {
        $post = request()->only(['question','answer','prompt','numbers','false_answer'],'post');

        $res = db('riddle_title')->where(['id'=>$id])->update($post);
    
        if ($res) {
            $this->success('操作成功','riddle/subject');
        }else{
            $this->error('操作失败');
        }
    }
    
    
    

          
    
}